package com.lai.shop.vo;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.List;

/**
 * @BelongsPackage: com.lai.shop.vo
 * @Author: laiciqi
 * @CreateTime: 2022-05-24  13:21
 * @Description: 购物车
 * @Version: 1.0
 */
@ApiModel(description = "购物车Vo")
public class CartVo { // <String (用户id),<Long (商品id) , cartItemVo>>

    /**
     * 商品列表
     */
    private List<CartItemVo> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 商品列表总价
     */
    private BigDecimal totalAmount;

    public Integer getCountNum() {
        return items != null && items.size() > 0 ?
                items.stream()
                        .filter(CartItemVo::getIsCheck)
                        .mapToInt(CartItemVo::getCount).sum() : 0;
    }

    public Integer getCountType() {
        return items.size();
    }

    //第一个参数是我们给出的初值，第二个参数是累加器，可以自己用实现接口完成想要的操作，这里使用Bigdecimal的add方法
    public BigDecimal getTotalAmount() {
        return items != null && items.size() > 0 ?
                items.stream().filter(CartItemVo::getIsCheck)
                .map(CartItemVo::getTotalPrice)
                .reduce(BigDecimal.ZERO,BigDecimal::add)
                : BigDecimal.ZERO;
    }

    public List<CartItemVo> getItems() {
        return items;
    }

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public CartVo() {
    }

    public CartVo(List<CartItemVo> items, Integer countNum, Integer countType, BigDecimal totalAmount) {
        this.items = items;
        this.countNum = countNum;
        this.countType = countType;
        this.totalAmount = totalAmount;
    }
}
